package com.cvit.cn.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cvit.cn.bean.Msg;
import com.cvit.cn.service.AcceptanceService;
import com.cvit.cn.test.ObjectExcelView;


/**
 *连表查询
 * @author 412
 *
 */
@Controller
public class AcceptanceController {
	@Autowired
	AcceptanceService acceptanceService;
	
	@Autowired 
	HttpServletRequest request;
	
	/**
	 * 关联查询数据（被选中供应商信息）
	 * @author LX
	 */
	@RequestMapping(value="/selectAcceptanceDatas")
	@ResponseBody
	public Msg selectAcceptanceDatas() throws Exception{
		List<Map<String,Object>> list=acceptanceService.selectAcceptanceDatas();
		return Msg.success().add("data", list);
	}
	
	/**
	 * 按供应商id查询关联表
	 */
	@RequestMapping(value="/selectAcceptanceById")
	@ResponseBody
	public Msg selectAcceptanceById(String id) throws Exception{
		List<Map<String,Object>> list=acceptanceService.selectAcceptanceById(id);
		return Msg.success().add("data", list);
	}
	
	
	
	
	/**
	 * 导出excel数据
	 * @author LX
	 */
	@RequestMapping(value="/exportExcel")
	@ResponseBody
	public ModelAndView exportExcel(String id){
		//dataMap用于封装全部数据，传给excel导出工具类ObjectExcelView
		Map<String,Object> dataMap=new HashMap<String,Object>();
		//titles用于存放excel标题
		List<String> titles=new ArrayList<String>();
		titles.add("供应商名称");
		titles.add("物品名称");
		titles.add("物品类别");
		titles.add("物品型号");
		titles.add("单位");
		titles.add("数量");
		titles.add("单价");
		titles.add("金额");
		titles.add("总价");
		titles.add("备注");
		//把要导出的excel标题titles存到dataMap中
		dataMap.put("titles", titles);
		//查询要导出的数据list，调用上面查询接口
		List<Map<String,Object>> list=acceptanceService.selectAcceptanceById(id);
		//varList用于存放下面封装好的每一个map，每一个map里面封装的是一个供应商实体类对象属性
		List<Map<String,String>> varList=new ArrayList<Map<String,String>>();
		//循环查出的数据list
		for(int i=0;i<list.size();i++){
			//把循环出来的每一条数据封装到map里
			Map<String,String> map=new HashMap<String,String>();
			//循环取出list里面的每一条数据封装给map，一条数据是一个供应商实体类对象，
			map.put("var1", list.get(i).get("supplier_name").toString());
			map.put("var2", list.get(i).get("commodity_name").toString());
			map.put("var3", list.get(i).get("commodity_category").toString());
			map.put("var4", list.get(i).get("specification_model").toString());
			map.put("var5", list.get(i).get("company").toString());
			map.put("var6", list.get(i).get("number").toString());
			map.put("var7", list.get(i).get("unit_price").toString());
			map.put("var8", list.get(i).get("money").toString());
			map.put("var9", list.get(i).get("total").toString());
			if(list.get(i).get("remarks")==null){
				map.put("var10", "");
			}else{
				map.put("var10", list.get(i).get("remarks").toString());
			}
			//把封装好的map放入varList里
			varList.add(map);
		}
		//把所有封装好的数据给dataMap
		dataMap.put("varList", varList);
		//调用excel导出工具类，
		ObjectExcelView erv=new ObjectExcelView();
		ModelAndView mv=new ModelAndView(erv,dataMap);
		return mv;
	}
	
	
	/**
	 * 假删除
	 */
	@RequestMapping(value="/delAcceptanceFalse")
	@ResponseBody
	public Msg delAcceptanceFalse(String acceptanceId){
		String[] ids = acceptanceId.split(",");
		if(acceptanceService.delAcceptanceFalse(ids)!=0){
			return Msg.success().add("success", "假删除订单表成功");
		}else{
			return Msg.fail().add("fail", "假删除订单表失败");
		}
		
	}
	
	
}
